//
//  ViewController.m
//  新浪登陆
//
//  Created by 石虎 on 2017/11/4.
//  Copyright © 2017年 shihu. All rights reserved.
//

#import "ViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDKUI.h>
#import <ShareSDKConnector/ShareSDKConnector.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 200, 150, 30);
    [btn setTitle:@"第三方新浪登陆" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    b.frame = CGRectMake(100, 400, 150, 30);
    [b setTitle:@"取消授权" forState:UIControlStateNormal];
    [b addTarget:self action:@selector(clickTwo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:b];
    
}

-(void)click{
    
    //    //例如微博的登录
    //    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo
    //           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
    //     {
    //         if (state == SSDKResponseStateSuccess)
    //         {
    //
    //             NSLog(@"uid=%@",user.uid);
    //             NSLog(@"%@",user.credential);
    //             NSLog(@"token=%@",user.credential.token);
    //             NSLog(@"昵称=%@",user.nickname);
    //             NSLog(@"头像=====%@",user.icon);
    //         }
    //
    //         else
    //         {
    //             NSLog(@"%@",error);
    //         }
    //
    //     }];
    
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"3.jpg"]];
    //    NSArray* imageArray = @[@"http://www.sinaimg.cn/lf/sports/logo85/144.png"];
    //    （注意：图片必须要在Xcode左边目录里面，名称必须要传正确，如果要分享网络图片，可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"]）
    if (imageArray) {
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:@"紫叶"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"http://mob.com"]
                                          title:@"hhhhhhhhh"
                                           type:SSDKContentTypeImage];
        //2、分享（可以弹出我们的分享菜单和编辑界面）
        [ShareSDK showShareActionSheet:nil
         //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                       
                       switch (state) {
                           case SSDKResponseStateSuccess:
                           {
                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                   message:nil
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"确定"
                                                                         otherButtonTitles:nil];
                               [alertView show];
                               break;
                           }
                           case SSDKResponseStateFail:
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@",error]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           default:
                               break;
                       }
                       
                       // [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                       //
                       //            if (state == SSDKResponseStateSuccess) {
                       //                NSLog(@"分享成功");
                       //            }
                       //        }];
                   }
         ];}
    
    
}
-(void)clickTwo{
    //取消微博登陆状态
    [ShareSDK cancelAuthorize:SSDKPlatformTypeSinaWeibo];
}



@end
